package gui.productgroup;

import gui.common.*;
import gui.inventory.*;
import hit.core.HomeInventoryTracker;
import hit.core.ProductContainer;
import hit.core.ProductGroup;
import hit.core.Size;
import hit.core.TreeFacade;
import hit.dao.DataAccessObject;
import hit.dao.FactoryReference;
import hit.util.SizeConverter;

/**
 * Controller class for the add product group view.
 */
public class AddProductGroupController extends Controller implements
		IAddProductGroupController {
	
	TreeFacade facade;
	ProductContainerData parent;
	Size newSize;
	
	/**
	 * Constructor.
	 * 
	 * @param view Reference to add product group view
	 * @param container Product container to which the new product group is being added
	 */
	public AddProductGroupController(IView view, ProductContainerData container) {
		super(view);
		
		facade = HomeInventoryTracker.Instance().GetTreeFacade();
		parent = container;
		newSize = new Size();
		
		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IAddProductGroupView getView() {
		return (IAddProductGroupView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() {
		
		try {
			ProductContainer container = (ProductContainer)parent.getTag(); 
			ProductGroup newGroup = new ProductGroup(getView().getProductGroupName());
			newGroup.SetParentContainer(container);
			newGroup.SetThreeMonth(newSize);
			
			getView().enableOK(container.CanAddProductGroup(newGroup));
		}
		catch (Exception ex) {
			getView().enableOK(false);
		}
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() {
		getView().setProductGroupName("");
		getView().setSupplyUnit(SizeUnits.Count);
		getView().setSupplyValue("0");
	}

	//
	// IAddProductGroupController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * add product group view is changed by the user.
	 */
	@Override
	public void valuesChanged() {
		
		try
		{	
			newSize = new Size();
			
			Size.Unit newUnit = SizeConverter.ConvertControllerUnitsToModel(
															getView().getSupplyUnit());
			newSize.SetType(newUnit);
			
			double newCount = Double.parseDouble(getView().getSupplyValue());
			newSize.SetAmount(newCount);
			
			enableComponents();
		}
		catch (Exception e) {
			newSize = null;
			enableComponents();
		}
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the add product group view.
	 */
	@Override
	public void addProductGroup() {
		
		try {
			ProductGroup group = facade.AddProductGroup(getView().getProductGroupName(), newSize, 
											(ProductContainer)parent.getTag());
			
			DataAccessObject dao = FactoryReference.GetFactory().CreateDataAccessObject();
			
			dao.StartTransaction();
			
			int id = dao.GetProductGroupDAO().Create(group);
			group.setId(id);
			
			dao.EndTransaction(true);
			
			dao.CloseConnection();
		}
		catch (Exception e) {
			getView().displayErrorMessage(e.toString());
		}
	}
}

